package com.Dao;

import cn.hutool.core.date.DateUtil;
import com.Bean.Good;
import com.Bean.Provider;
import com.Bean.User;
import com.Common.Constant;
import com.Common.PageInfo;
import com.Util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: hkw14
 * @time: 2021/5/2 21:06
 */
public class GoodDao extends BaseDao{
    public PageInfo<Good> selectPage(Map<String,Object> params, Integer page, Integer limit) {
        String sql = "select * from `goods` where";
        if(params.containsKey("goodName")) {
            sql = sql + "  goodName like '%"+params.get("goodName")+"%' and";
        }
        if(params.containsKey("providerName")) {
            sql = sql + "  providerName like '%"+params.get("providerName")+"%'   and";
        }
        if(params.containsKey("goodsType")) {
            sql = sql + "  goodsType like '%"+params.get("goodsType")+"%' and";
        }
        if(params.containsKey("brand")) {
            sql = sql + "  brand like '%"+params.get("brand")+"%' and";
        }
        if(sql.endsWith("and")) {
            sql = sql.substring(0,sql.lastIndexOf("and"));
        }
        if(sql.endsWith("where")) {
            sql = sql.substring(0,sql.lastIndexOf("where"));
        }
        return this.selectPage(sql,Good.class, page, limit);
    }
    public boolean add(Integer ID,Integer number) {
        String sql = "UPDATE goods SET number = number + ? WHERE ID = ?;";
        boolean update = this.update(sql, number, ID);
        return update;
    }
    public boolean reduce(Integer ID,Integer number) {
        String sql = "UPDATE goods SET number = number - ? WHERE ID = ?;";
        boolean update = this.update(sql, number, ID);
        return update;
    }

    public boolean insertGood(String goodName,String provider,String goodType,String brand,String description) {
        String sql = "INSERT INTO  goods (goodName, providerName, description, number, goodsType ,brand,averagePurchasePrice) VALUES (?,?,?,0,?,?,0);";
        int userId = this.insert(sql,goodName,provider,description,goodType,brand);
        return userId > 0 ?true:false;
    }

    public boolean batchDelete(String[] ids) {
        Connection conn = JdbcUtil.getConn();
        PreparedStatement prep = null;
        String sql = "DELETE FROM goods WHERE ID=?;";

        try {
            //设置事务为手动提交
            conn.setAutoCommit(false);
            prep = conn.prepareStatement(sql);
            for (String id : ids) {
                prep.setInt(1, Integer.parseInt(id));
                prep.addBatch();
            }
            int[] count = prep.executeBatch();
            //手动提交事务
            conn.commit();
            return count.length > 0 ?true:false;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, prep);
        }
        return false;
    }

    public List<Good> getGoodList(){
        String sql = "select * from `goods`";
        List<Good> providers = this.selectList(sql, Good.class);
        return providers;
    }

    public Good getGoodByID(Integer goodID){
        String sql = "select * from `goods` where ID = ?";
        Good good = this.selectOne(sql, Good.class, goodID);
        return good;
    }
}
